package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentSearchServiceBinding;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter6;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.viewmodel.enterprise.SearchServiceModel;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;


public class SearchServiceFragment extends AppLazyFragment {

    private SearchServiceModel mViewModel;
    private FragmentSearchServiceBinding binding;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int index = 0;

    public static SearchServiceFragment newInstance() {
        SearchServiceFragment fragment = new SearchServiceFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_search_service, container, false);
        return binding.getRoot();
    }


    @Override
    protected void initView() {
        mViewModel = new ViewModelProvider(getActivity()).get(SearchServiceModel.class);
        binding.setViewModel(mViewModel);
        mViewModel.getServiceType();
        initObsever();
    }

    @Override
    protected void onLazyLoad() {

    }


    private void initObsever() {
        mViewModel.typeList.observe(getActivity(), list -> {
            ParentAreaEntity data = new ParentAreaEntity();
            data.setName("全部");
            data.setId("");
            list.add(0, data);
            for (int i = 0; i < list.size(); i++) {
                mFragments.add(ProfessionalQuoteFragment.newInstance(list.get(i).getChildren(), list.get(i).getId()));
            }
            CommPagerAdapter6 pagerAdapter = new CommPagerAdapter6(getChildFragmentManager(), mFragments, list);
            binding.viewpager.setOffscreenPageLimit(list.size());
            binding.viewpager.setAdapter(pagerAdapter);
            binding.tabLayout.setupWithViewPager(binding.viewpager);
            binding.tabLayout.getTabAt(index).select();
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LoginEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PROFESSIONAL_QUOTE_FRG_JUMP) {
        }
    }
}
